<template>
  <div>
    <el-card style="margin:20px 0 10px 0">
      <mer-search :searchConfig="formConfig" :searchBar="searchBar"
        @search="search"
        @reset="reset(initQuery)"
        @changeTime="value => changeTime(value,'searchBar')"
        :loading="tableLoading">
      </mer-search>
    </el-card>

    <el-card style="padding: 0 0 20px 0">
      <el-table
        :data="tableData"
        stripe
        size="small"
        v-loading="loading"
        @expand-change="expandChange"
        :row-key="({serialNo}) => serialNo"
        :expand-row-keys="expandList"
        style="width: 100%">
        <el-table-column type="expand">
          <template slot-scope="props">
            <div>
              <div class="expand header">
                <span>订单号</span>
                <span>油号</span>
                <span>实付金额</span>
              </div>
              <div class="expand">
                <div v-for="item in expandData[props.row.serialNo]" :key="item.orderNo">
                  <span>{{ item.orderNo }}</span>
                  <span>{{ item.oilNo }}</span>
                  <span>{{ item.amountPay }}</span>
                </div>
              </div>
            </div>
          </template>
        </el-table-column>
        <el-table-column
          label="发票申请时间"
          align="center"
          prop="createTime">
        </el-table-column>
        <el-table-column
          label="用户手机号"
          align="center"
          prop="buyerPhone">
        </el-table-column>
        <el-table-column
          label="开票总金额"
          align="center"
          prop="invoiceTotalPriceTax">
        </el-table-column>
        <el-table-column
          label="开票状态"
          align="center"
          prop="invoiceStatusName">
        </el-table-column>
        <el-table-column
          label="处理完成时间"
          align="center"
          prop="finishTime">
        </el-table-column>
        <el-table-column
          label="操作"
          align="center"
          width="110">
          <template slot-scope="{row}">
            <el-button v-if="row.invoiceStatus === 0" class="table-mini-btn" @click="confirm(row.serialNo)">确认开票</el-button>
            <el-button v-if="row.invoiceStatus === 1" class="table-mini-btn" @click="revoke(row.serialNo)">撤销开票</el-button>
          </template>
        </el-table-column>
      </el-table>
      <div class="pagination">
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="pageChange"
          :current-page="pagination.pageIndex"
          :page-size="pagination.pageSize"
          :page-sizes="[10, 50, 100, 200]"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total">
        </el-pagination>
      </div>
    </el-card>

  </div>
</template>
<script>
import listPage from '@/mixins/listPage'
import { getInvoiceList, confirmInvoice, revokeInvoice, getStatusList, getDetail } from "@/api/finance/userApplyInvoice";

// 表单参数配置
let formConfig = [
  { label: '发票申请时间', type: 'date', modelName: 'tradeTime', model: '', dateType: 'datetimerange', format: 'yyyy-MM-dd HH:mm:ss', clearable: true, labelWidth: '110px' },
  { label: '用户手机号', type: 'input', modelName: 'buyerPhone', labelWidth: '100px' },
  { label: '订单号', type: 'input', modelName: 'orderNo' },
  { label: '开票状态', type: 'select', modelName: 'invoiceStatus', selectOptions: [], define: true, selectLabel: 'message', selectValue: 'code' }
]

export default {
  name: 'userApplyInvoice',
  mixins: [listPage],
  data () {
    return {
      formConfig,
      tableData: [],
      pagination: {
        pageSize: 10,
        pageIndex: 1
      },
      total: 0,
      loading: false,
      expandData: {},
      expandList: []
    }
  },
  methods: {
    async initStatusList () {
      try {
        const {code, result} = await getStatusList()
        if (code !== 200) return
        this.formConfig[this.findConfigIndex(this.formConfig, 'invoiceStatus')].selectOptions = result
      } catch (error) {
        console.log(error)
      }
    },
    // 初始化列表
    async search () {
      this.loading = true
      const params = {
        ...this.pagination,
        ...this.searchBar
      }
      try {
        const { code, result, totalRow } = await getInvoiceList(params)
        if (code !== 200) return
        this.tableData = result || []
        this.total = totalRow
      } catch (error) {
        console.log(error)
      } finally {
        this.loading = false
      }
    },
    initQuery () {
      this.formConfig[this.findConfigIndex(this.formConfig, 'tradeTime')].model = ''
      this.searchBar.startTime = ''
      this.searchBar.endTime = ''
    },
    handleSizeChange (val) {
      this.pagination.pageSize = val
      this.search()
    },
    pageChange (val) {
      this.pagination.pageIndex = val
      this.search()
    },
    confirm (serialNo) {
      this.$confirm('是否确认开票', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async () => {
        const {code, message} = await confirmInvoice({ serialNo })
        if (code !== 200) {
          this.$message.error(message)
        } else {
          this.$message.success('开票成功')
          this.search()
        }
      })
    },
    revoke (serialNo) {
      this.$confirm('是否撤销开票', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async () => {
        const {code, message} = await revokeInvoice({ serialNo })
        if (code !== 200) {
          this.$message.error(message)
        } else {
          this.$message.success('撤销成功')
          this.search()
        }
      })
    },
    async expandChange (rows) {
      const serialNo = rows.serialNo
      if (this.expandData[serialNo]) return
      try {
        const {code, result} = await getDetail({ serialNo })
        if (code !== 200) return
        this.expandData[serialNo] = result || []
        this.expandList = [rows.serialNo]
      } catch (error) {
        console.log(error)
      }
    },
  },
  created () {
    this.initStatusList()
    this.search()
  }
}
</script>
<style lang="less" scoped>
  .expand {
    span {
      display: inline-block;
      width: 200px;
      height: 40px;
      line-height: 40px;
      text-align: center;
    }
    &.header {
      border-bottom: 1px solid #eee;
    }
  }
  .pagination {
    margin-top: 20px;
    float: right;
  }
</style>